package ua.amsoft.brelok;

import java.util.List;

import ua.amsoft.brelok.dto.DepartamentUsageResultReport;
import ua.amsoft.brelok.dto.DepartamentUsageResultReports;
import ua.amsoft.brelok.loader.DepartamentUsageReportsTaskLoader;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class StatisticsFragment extends Fragment implements
        LoaderCallbacks<DepartamentUsageResultReports> {
    
    private ProgressBar progressBar;
    
    private TextView dataStatus;
    
    private RelativeLayout relativeLayout;
    
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
            final Bundle savedInstanceState) {
        Log.i(getTag(), "onCreateView() started");
        final View view = inflater.inflate(R.layout.fragment_statistics, container, false);
        return view;
    }
    
    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        Log.i("Loader", "onActivityCreated() started");
        super.onActivityCreated(savedInstanceState);
        this.progressBar = (ProgressBar) getView().findViewById(R.id.statistics_progress_bar);
        this.dataStatus = (TextView) getView().findViewById(R.id.statistics_data_status);
        
        this.relativeLayout = (RelativeLayout) getView().findViewById(
                R.id.fragment_statistics_layout);
        this.progressBar.setVisibility(View.VISIBLE);
        this.dataStatus.setVisibility(View.VISIBLE);
        getLoaderManager().initLoader((int) (Math.random() * 10000), null, this);
    }
    
    @Override
    public Loader<DepartamentUsageResultReports> onCreateLoader(final int arg0, final Bundle arg1) {
        Log.i("Loader", "onCreateLoader() started");
        return new DepartamentUsageReportsTaskLoader(getActivity());
    }
    
    @Override
    public void onLoadFinished(final Loader<DepartamentUsageResultReports> loader,
            final DepartamentUsageResultReports data) {
        Log.i("Loader", "onLoadFinished() started");
        final TableLayout layout = (TableLayout) getView().findViewById(R.id.statistics_table);
        try {
            if (data != null) {
                final List<DepartamentUsageResultReport> reportList = data.getReports();
                buildTable(layout, reportList);
                final LayoutParams layoutParams = new LayoutParams(
                        android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                this.relativeLayout.setLayoutParams(layoutParams);
                this.progressBar.setVisibility(View.INVISIBLE);
                this.dataStatus.setVisibility(View.INVISIBLE);
            }
        } catch (final Exception e) {
            Log.e("StatisticsFragment error", e.getLocalizedMessage());
        }
        loader.abandon();
    }
    
    /**
     * @param layout - {@link TableLayout}
     * @param reportList - reports object
     */
    private void buildTable(final TableLayout layout,
            final List<DepartamentUsageResultReport> reportList) {
        for (final DepartamentUsageResultReport departamentUsageResultReport : reportList) {
            final TableRow tableRow = new TableRow(getActivity());
            
            final TextView executiveId = new TextView(getActivity());
            executiveId.setText(String.valueOf(departamentUsageResultReport.getExecutiveId()));
            executiveId.setBackgroundResource(R.drawable.table_cell);
            executiveId.setTextColor(getResources().getColor(R.color.jelly_bean));
            
            final TextView executiveName = new TextView(getActivity());
            executiveName.setText(departamentUsageResultReport.getExecutiveName());
            executiveName.setBackgroundResource(R.drawable.table_cell);
            executiveName.setTextColor(getResources().getColor(R.color.jelly_bean));
            tableRow.addView(executiveId);
            tableRow.addView(executiveName);
            layout.addView(tableRow);
        }
    }
    
    @Override
    public void onLoaderReset(final Loader<DepartamentUsageResultReports> loader) {
        Log.i("Loader", "onLoaderReset() started");
        loader.reset();
    }
}